package cn.hp.realm;

import java.util.Arrays;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.realm.AuthenticatingRealm;

import cn.hp.domain.User;
import cn.hp.service.UserService;
import cn.hp.service.UserServiceImpl;

public class UserRealm extends AuthenticatingRealm{
	
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return this.getClass().getName();
	}
	
	private UserService userService = new UserServiceImpl();

	//完成用户认证
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		//获取当前登录的用户名
		String username = token.getPrincipal().toString();
		System.out.println(username);
		//获取当前登录的用户密码
		//Object password = token.getCredentials();
		//System.out.println(Arrays.toString((char[])password));
		
		//根据用户名取数据库查询用户信息
		User user = userService.findUser(username);
		if(user != null) {
			//判断密码是否正确
			//p1：用户身份  p2：数据库里面用户的密码  p3：当前类名
			SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, user.getPassword(), getName());
			return info;
		}
		
		return null;
	}

}
